package com.wuliu.system.service.applet.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuliu.common.core.domain.PageResult;
import com.wuliu.common.core.domain.entity.BasCust;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.common.utils.StringUtils;
import com.wuliu.system.domain.dto.applet.BasCustAddressDto;
import com.wuliu.system.domain.dto.applet.QryCustAddressDto;
import com.wuliu.system.service.applet.IBasCustAddressService;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.BasCustAddressMapper;
import com.wuliu.system.domain.BasCustAddress;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 小程序用户地址库Service业务层处理
 *
 * @author realue
 * @date 2023-11-01
 */
@Service
public class BasCustAddressServiceImpl extends ServiceImpl<BasCustAddressMapper, BasCustAddress> implements IBasCustAddressService {
    @Resource
    private BasCustAddressMapper basCustAddressMapper;

    /**
     * 查询小程序用户地址库
     *
     * @param id 小程序用户地址库主键
     * @return 小程序用户地址库
     */
    @Override
    public BasCustAddress selectBasCustAddressById(String id) {
        return basCustAddressMapper.selectBasCustAddressById(id);
    }

    /**
     * 查询小程序用户地址库列表
     *
     * @param basCustAddress 小程序用户地址库
     * @return 小程序用户地址库
     */
    @Override
    public List<BasCustAddress> selectBasCustAddressList(BasCustAddress basCustAddress) {
        return basCustAddressMapper.selectBasCustAddressList(basCustAddress);
    }

    /**
     * 新增小程序用户地址库
     *
     * @param dto 小程序用户地址库
     * @return 结果
     */
    @Override
    public int insertBasCustAddress(BasCustAddressDto dto) {
        BasCust basCust = SecurityUtils.getBasCust();
        BasCustAddress basCustAddress = new BasCustAddress();
        BeanUtil.copyProperties(dto, basCustAddress);
        basCustAddress.setId(IdUtil.randomUUID());
        basCustAddress.setCustId(basCust.getId());
        basCustAddress.setCreateBy(basCust.getCustName());
        basCustAddress.setCreateTime(DateUtils.getNowDate());
        basCustAddress.setSysAccount(basCust.getSysAccount());
        basCustAddress.setSysAccount(basCustAddress.getSysAccount());
        if (dto.getDefaultFlag() == 1) {
            QueryWrapper<BasCustAddress> wrapper = new QueryWrapper<>();
            wrapper.eq("sys_account", basCust.getSysAccount())
                    .eq("type", dto.getType())
                    .eq("cust_id", basCust.getId());
            BasCustAddress upAddress = new BasCustAddress();
            upAddress.setDefaultFlag(0);
            basCustAddressMapper.update(upAddress, wrapper);
        }
        return basCustAddressMapper.insertBasCustAddress(basCustAddress);
    }

    /**
     * 修改小程序用户地址库
     *
     * @param dto 小程序用户地址库
     * @return 结果
     */
    @Override
    public int updateBasCustAddress(BasCustAddressDto dto) {
        if (StringUtils.isEmpty(dto.getId())) {
            throw new ServiceException("id不能为空");
        }
        BasCust basCust = SecurityUtils.getBasCust();
        BasCustAddress basCustAddress = new BasCustAddress();
        BeanUtil.copyProperties(dto, basCustAddress);
        basCustAddress.setUpdateTime(DateUtils.getNowDate());
        basCustAddress.setUpdateBy(basCust.getCustName());
        if (dto.getDefaultFlag() == 1) {
            QueryWrapper<BasCustAddress> wrapper = new QueryWrapper<>();
            wrapper.eq("sys_account", basCust.getSysAccount())
                    .eq("type", dto.getType())
                    .eq("cust_id", basCust.getId());
            BasCustAddress upAddress = new BasCustAddress();
            upAddress.setDefaultFlag(0);
            basCustAddressMapper.update(upAddress, wrapper);
        }
        return basCustAddressMapper.updateBasCustAddress(basCustAddress);
    }

    /**
     * 批量删除小程序用户地址库
     *
     * @param ids 需要删除的小程序用户地址库主键
     * @return 结果
     */
    @Override
    public int deleteBasCustAddressByIds(String[] ids) {
        return basCustAddressMapper.deleteBasCustAddressByIds(ids);
    }

    /**
     * 删除小程序用户地址库信息
     *
     * @param id 小程序用户地址库主键
     * @return 结果
     */
    @Override
    public int deleteBasCustAddressById(String id) {
        return basCustAddressMapper.deleteBasCustAddressById(id);
    }

    /**
     * 查询会员地址库
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<BasCustAddress> selectList(QryCustAddressDto dto) {
        BasCust basCust = SecurityUtils.getBasCust();
        QueryWrapper<BasCustAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("sys_account", basCust.getSysAccount())
                .eq("cust_id", basCust.getId())
                .eq("type", dto.getType());
        if (StringUtils.isNotEmpty(dto.getKeyValue())) {
            wrapper.and(qw -> qw.eq("tel", dto.getKeyValue())
                    .or().like("comp_name", dto.getKeyValue())
                    .or().like("name", dto.getKeyValue()));
        }
        wrapper.orderByDesc("default_flag", "create_time");
        Page page = new Page(dto.getPageNum(), dto.getPageSize());
        basCustAddressMapper.selectPage(page, wrapper);
        return new PageResult<>(page);
    }
}
